<?php
namespace app\api\controller\salesman;

use think\Controller;
use think\Request;
use think\Db;
use app\common\model\power\Rolepower;
use app\common\model\user\User as UserModel;
use app\common\model\device\Device as DeviceModel;
use app\api\controller\Base;
use think\facade\Env;
use app\api\controller\Pub;
class Device extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->is_login();
        Rolepower::powerguard('305002'); // 设备管理
    }

    private function is_login()
    {
        $data =UserModel::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
    
    //工厂列表
    public function factoryList()
    {
        $factoryData=Db::name('factory')->select();
        if($factoryData){
            return myok($factoryData);
        }else{
            return myerr('工厂列表为空，请先维护工厂信息');
        }
    }

    //产品列表
    public function deviceList()
    {
        $devices = Db::name('device')->where('id','>',1)->select();
        $accessories = Db::name('accessories')->select();
        $productMap = [];
        foreach ($devices as $device) {
            $device['accessories'] = [];
            $productMap[$device['id']] = $device;
        }
        foreach ($accessories as $accessory) {
            $deviceId = $accessory['device_id'];
            if (isset($productMap[$deviceId])) {
                $productMap[$deviceId]['accessories'][] = $accessory;
            }
        }
        $result = array_values($productMap);
        return myok($result);
    }

    public function deviceAdd()
    {
        $data=input('post.');
        if (empty($data['name']) || empty($data['factory_id']) || empty($data['is_need'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $res=Db::name('device')->insert($data);
        if($res){
            return myok('设备添加成功');
        }else{
            return myerr('设备添加失败');
        }
    }

    public function deviceEdit()
    {
        $data=input('post.');
        if (empty($data['id']) || empty($data['name']) || empty($data['factory_id']) || empty($data['is_need'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $res=Db::name('device')->where('id',$data['id'])->update($data);
        if($res){
            return myok('设备修改成功');
        }else{
            return myerr('设备修改失败');
        }
    }

    public function deviceDel()
    {
        $device_id=input('id');
        if (empty($device_id)) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        DeviceModel::destroy($device_id);
        return json(['code'=>1, 'msg'=>'设备删除成功']);

    }

    public function accessoryAdd()
    {
        $data=input('post.');
        if(empty($data['name'] || empty($data['device_id']))){
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $res=Db::name('accessories')->insert($data);
        if($res){
            return myok('配件添加成功');
        }else{
            return myerr('配件添加失败');
        }

    }

    public function accessoryEdit()
    {
        $data=input('post.');
        if (empty($data['id']) || empty($data['name']) || empty($data['device_id'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $res=Db::name('accessories')->where('id',$data['id'])->update($data);
        if($res){
            return myok('配件修改成功');
        }else{
            return myerr('配件修改失败');
        }
    }

    public function accessoryDel()
    {
        $accessory_id=input('id');
        if (empty($accessory_id)) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $res=Db::name('accessories')->where('id',$accessory_id)->delete();
        return json(['code'=>1, 'msg'=>'配件删除成功']);
    }
}  